package com.Jano.service.Impl;

import com.Jano.dao.Impl.UserDaoImpl;
import com.Jano.dao.UserDao;
import com.Jano.dto.FriendDto;
import com.Jano.entity.User;
import com.Jano.exception.BusinessException;
import com.Jano.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.Jano.constant.BusinessConstant.REDIS_HOST;
import static com.Jano.constant.BusinessConstant.REDIS_POST;

/**
 * description: UserServiceImpl <br>
 * date: 2022/6/10 4:30 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
public class UserServiceImpl extends AbstractBaseServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String userName, String passWord) {
        if (StringUtils.isBlank(userName)) {
            throw new BusinessException("用户名不能为空");
        }
        if (StringUtils.isBlank(passWord)) {
            throw new BusinessException("密码不能为空");
        }
        User userLogin = userDao.login(userName, passWord);
        if (Objects.isNull(userLogin)) {
            throw new BusinessException("用户不存在");
        }
        return userDao.login(userName, passWord);
    }

    @Override
    public User autoLogin(String cookieUserInfo) {
        if (StringUtils.isBlank(cookieUserInfo)){
            return null;
        }
        List<User> users = userDao.selectAllUsers();
        User loginUser = null;
        for (User user : users) {
            String userName = user.getUserName();
            String userPwd = user.getPassWord();
            if (StringUtils.equals(cookieUserInfo, DigestUtils.md5Hex(userName + "," + userPwd))) {
                loginUser = user;
                break;
            }
        }
        if (Objects.nonNull(loginUser)) {
            return loginUser;
        }
        return null;
    }

    @Override
    public void register(String userName, String passWord, String pwdConfirm) {
        throwIfTrue(StringUtils.isBlank(userName), "用户名不能为空");
        throwIfTrue(userDao.isExist(userName), "用户名已存在");
        throwIfFalse(passWord.equals(pwdConfirm), "两次密码不相等");
        userDao.register(userName, passWord);
    }

    @Override
    public List<FriendDto> getCommonFriendsIdByUserId(Integer firstUserId, Integer secondUserId) {
        try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
             Jedis jedis = jedisPool.getResource()) {
            if (firstUserId.equals(secondUserId)) {
                throw new BusinessException("不能自我查询好友");
            }
            List<FriendDto> firstFriendDtos = userDao.selectFriendsIdByUserId(firstUserId);
            List<FriendDto> secondFriendDtos = userDao.selectFriendsIdByUserId(secondUserId);
            List<FriendDto> commonFriendDtos = new ArrayList<>();
            firstFriendDtos.stream().forEach(friend -> jedis.sadd(firstUserId.toString(), friend.getFriendId().toString()));
            secondFriendDtos.stream().forEach(friend -> jedis.sadd(secondUserId.toString(), friend.getFriendId().toString()));
            Set<String> sinters = jedis.sinter(firstUserId.toString(), secondUserId.toString());
            for (String sinter : sinters) {
                List<FriendDto> collect = firstFriendDtos.stream().filter(friend -> friend.getFriendId().toString().equals(sinter)).collect(Collectors.toList());
                commonFriendDtos.addAll(collect);
            }
            return commonFriendDtos;
        }
    }
}
